package com.fresh.bus.chat.business;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.support.collections.RedisZSet;
import org.springframework.stereotype.Component;

import com.fresh.bus.canoe.message.Response;
import com.fresh.bus.canoe.session.Session;
import com.fresh.bus.canoe.session.SessionRoleManager;
import com.fresh.bus.chat.ctrl.OpCodes;
import com.fresh.bus.chat.domain.UserLog;
import com.fresh.bus.chat.persist.MessageDao;
import com.fresh.bus.chat.persist.UserDao;

@Component
public class MessageService {

	public static final short GET_FEIJI = 2;
	public static final short ARRIVAL_FEIJI = 3;
	public static final short ADD_STAR_FEIJI = 4;
	public static final short FOLLOW_USER = 5;
	public static final short WAIT_FEIJI = 6;
	public static final short GET_FLOWER = 7;
	public static final short MEET_FROM_FEIJI = 8;
	public static final short GET_QQ = 9;
	public static final short SAY_HELLO = 10;

	private static final Logger userLog = LoggerFactory.getLogger("userLog");

	@Resource
	protected SessionRoleManager roleManager;

	@Resource
	private MessageDao dao;
	@Resource
	private UserService userService;
	@Resource
	private UserLogService userLogService;

	@Resource
	private ScheduleManager scheduleManager;

	private StringRedisTemplate template;

	@Autowired
	public MessageService(StringRedisTemplate template) {
		this.template = template;
	}

	public void notifyTargetUser(String user, String from, int type,
			String msgKey) {
		// 记录日志
		MDC.put("actionDesc", from + "," + user + "," + msgKey);
		userLog.info(String.valueOf(OpCodes.SYN_USER_LOG) + type);

		String newLog = this.userLogService.updateLog(user, from, type, msgKey);
		userService.saveUser(user);
		Session session = this.roleManager.getSession(user);
		if (session != null) {
			Response response = new Response(OpCodes.SYN_USER_LOG);
			response.setData(newLog);
			session.send(response);
		}
	}

	// 世界广播
	public void sendAll(String room, int type, String content) throws Exception {
		Map<Integer, String> map = new HashMap<Integer, String>();
		map.put(type, content);

		BoundHashOperations<String, String, String> ops = template
				.boundHashOps(KeyUtils.rid(room));
		RedisZSet<String> mr = this.scheduleManager.males(room);
		RedisZSet<String> fr = this.scheduleManager.females(room);
		Set<String> all = new HashSet<String>();
		all.addAll(mr);
		all.addAll(fr);
		Response response;
		response = new Response(OpCodes.BROADCAST_MSG);
		for (String i : all) {
			response.setData(map);
			roleManager.getSession(i).send(response);
		}
	}
}
